from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModBureauWeeklyRepSum import BureauWeeklyRepSum, bureauWeeklyRepSumSchema
from database import Session, engine
import datetime
from sqlalchemy import Date, or_


def get_weeklyrep_info(session, bureauid: str, monthnum: Date, weeknum: int):
    # 县局操作员-县局工作总结周报(查询审核已通过的信息)
    repinfo = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.bureauid == bureauid).filter(
        BureauWeeklyRepSum.monthnum == monthnum).filter(
        BureauWeeklyRepSum.weeknum == weeknum).filter(BureauWeeklyRepSum.flag == "2").one_or_none()
    return JSONResponse(bureauWeeklyRepSumSchema.dump(repinfo, many=False).data)


def get_weeklyrep_write_info(session, bureauid: str, monthnum: Date, weeknum: int):
    # 项目经理-县局工作总结周报
    repinfo = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.bureauid == bureauid).filter(
        BureauWeeklyRepSum.monthnum == monthnum).filter(BureauWeeklyRepSum.weeknum == weeknum).one_or_none()
    return JSONResponse(bureauWeeklyRepSumSchema.dump(repinfo, many=False).data)


def get_weeklyrep_check_info(session, bureauid: str, monthnum: Date, weeknum: int):
    # 公司监察部-县局工作总结周报详细信息
    repinfo = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.bureauid == bureauid).filter(
        BureauWeeklyRepSum.monthnum == monthnum).filter(
        BureauWeeklyRepSum.weeknum == weeknum).filter(BureauWeeklyRepSum.flag == "1").one_or_none()
    return JSONResponse(bureauWeeklyRepSumSchema.dump(repinfo, many=False).data)


def get_weeklyrep_check_list(session):
    # 公司监察部-县局工作总结周报列表
    replist = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.flag == "1").order_by(
        BureauWeeklyRepSum.entry_time.asc()).all()
    return JSONResponse(bureauWeeklyRepSumSchema.dump(replist, many=True).data)


def update_weekly_info_check(session, dict_bwrs: dict):
    # 审核页面更新审核是否通过及原因
    try:
        bureauid = dict_bwrs.get("bureauid")
        monthnum = dict_bwrs.get("monthnum")
        weeknum = dict_bwrs.get("weeknum")
        bwrs = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.bureauid == bureauid).filter(
            BureauWeeklyRepSum.monthnum == monthnum).filter(
            BureauWeeklyRepSum.weeknum == weeknum).one_or_none()
        bwrs.flag = dict_bwrs.get("flag")
        bwrs.remark = dict_bwrs.get("remark")
    except Exception as e:
        raise


def update_weekly_info_write(session, dict_bwrs: dict, entry_openid: str, entry_name: str, entry_time: datetime):
    # 周工作总结填报更新
    try:
        bureauid = dict_bwrs.get("bureauid")
        monthnum = dict_bwrs.get("monthnum")
        weeknum = dict_bwrs.get("weeknum")
        bwrs = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.bureauid == bureauid).filter(
            BureauWeeklyRepSum.monthnum == monthnum).filter(
            BureauWeeklyRepSum.weeknum == weeknum).one_or_none()
        bwrs.flag = dict_bwrs.get("flag")
        bwrs.target_analysis = dict_bwrs.get("target_analysis")
        bwrs.lastweek_worksum = dict_bwrs.get("lastweek_worksum")
        bwrs.week_workplan = dict_bwrs.get("week_workplan")
        bwrs.teamwork = dict_bwrs.get("teamwork")
        bwrs.entry_openid = entry_openid
        bwrs.entry_name = entry_name
        bwrs.entry_time = entry_time
    except Exception as e:
        raise


def get_weekcheck_count(session, bureauid: str, flagid: str):
    # 查询1周报正在填报或者审核退回状态的数量2正在审核状态的数量(角标)
    if flagid == "1":
        if bureauid == "00":
            week_count = session.query(BureauWeeklyRepSum).filter(
                or_(BureauWeeklyRepSum.flag == "0", BureauWeeklyRepSum.flag == "3")).all()
            list_week = bureauWeeklyRepSumSchema.dump(week_count, many=True).data
        else:
            week_count = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.bureauid == bureauid).filter(
                or_(BureauWeeklyRepSum.flag == "0", BureauWeeklyRepSum.flag == "3")).all()
            list_week = bureauWeeklyRepSumSchema.dump(week_count, many=True).data
    else:
        week_count = session.query(BureauWeeklyRepSum).filter(BureauWeeklyRepSum.flag == "1").all()
        list_week = bureauWeeklyRepSumSchema.dump(week_count, many=True).data
    return len(list_week)

def get_weeklyrep_list(session, monthnum: Date):
    # 工作总结周报列表
    replist = session.query(BureauWeeklyRepSum).filter(
        BureauWeeklyRepSum.monthnum == monthnum).order_by(
        BureauWeeklyRepSum.weeknum.desc(), BureauWeeklyRepSum.bureauid.asc()).all()
    return JSONResponse(bureauWeeklyRepSumSchema.dump(replist, many=True).data)